package com.xposed.aliproxy.xposed;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.TextView;

import com.xposed.aliproxy.qrcode.GlobalInstance;
import com.xposed.aliproxy.utils.Utils;
import com.xposed.aliproxy.utils.XposeUtil;
import com.xposed.aliproxy.xlistener.XposedAttachedListener;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;

public class Tutorial implements IXposedHookLoadPackage, XposedAttachedListener {
    public boolean aliHooked = false;
    public BroadcastReceiver broadcastReceiver = new C06586();
    private AliHookMethod hookMethod;

    class C06586 extends BroadcastReceiver {
        C06586() {
        }

        @Override
        public synchronized void onReceive(Context context, Intent intent) {
            try {
                String action = intent.getAction();
                if ("com.abcd.action.xposedaction.generateqrcode".equals(action)) {
                    action = intent.getStringExtra("money");
                    String stringExtra = intent.getStringExtra("remark");
                    Intent intent2 = new Intent(context, XposedHelpers.findClass("com.alipay.mobile.payee.ui.PayeeQRSetMoneyActivity", context.getClassLoader()));
                    intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent2.putExtra("mark", stringExtra);
                    intent2.putExtra("money", action);
                    context.startActivity(intent2);
                } else if ("com.abcd.action.xposedaction.unregister".equals(action)) {
                    context.unregisterReceiver(this);
                }
            } catch (Exception e) {
                e.printStackTrace();
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("exception->");
                stringBuilder.append(e.getLocalizedMessage());
                XposedBridge.log(stringBuilder.toString());
            }
            return;
        }
    }

    // ------------------------- Hook  CallBack ----------------------------------------
    class SystemClockCallback extends XC_MethodHook {
        @Override
        protected void beforeHookedMethod(MethodHookParam methodHookParam) throws Throwable {
        }

        @Override
        protected void afterHookedMethod(MethodHookParam methodHookParam) throws Throwable {
            TextView textView = (TextView) methodHookParam.thisObject;
            if (GlobalInstance.getInstance().f1765d) {
                textView.setText("在线");
                textView.setTextColor(-16711936);
                return;
            }
            textView.setText("离线");
            textView.setTextColor(-65536);
        }
    }

    class DeviceIdCallback extends XC_MethodHook {
        @Override
        protected void afterHookedMethod(MethodHookParam methodHookParam) throws Throwable {
            String a = XposeUtil.m2992a("deviceId");
            if (!Utils.isNull(a)) {
                methodHookParam.setResult(a);
            }
            super.afterHookedMethod(methodHookParam);
        }
    }

    class ImeiCallback extends XC_MethodHook {
        @Override
        protected void afterHookedMethod(MethodHookParam methodHookParam) throws Throwable {
            String a = XposeUtil.m2992a("imei");
            if (!Utils.isNull(a)) {
                methodHookParam.setResult(a);
            }
            super.afterHookedMethod(methodHookParam);
        }
    }

    class HardwareAddress extends XC_MethodHook {
        @Override
        protected void afterHookedMethod(MethodHookParam methodHookParam) throws Throwable {
            String a = XposeUtil.m2992a("mac");
            if (Utils.isNull(a)) {
                byte[] bArr = (byte[]) methodHookParam.getResult();
                if (bArr != null && bArr.length != 0) {
                    methodHookParam.setResult(XposeUtil.m2993a(bArr).getBytes());
                } else {
                    return;
                }
            }
            methodHookParam.setResult(a.getBytes());
            super.afterHookedMethod(methodHookParam);
        }
    }

    class MacAddress extends XC_MethodHook {
        @Override
        protected void afterHookedMethod(MethodHookParam methodHookParam) throws Throwable {
            String a = XposeUtil.m2992a("mac");
            if (!Utils.isNull(a)) {
                methodHookParam.setResult(a);
            }
            super.afterHookedMethod(methodHookParam);
        }
    }

    @Override
    public void onComplete() {
    }

    /**
     * App启动时候调用: 需筛选需要hook的app的包名
     */
    @Override
    public void handleLoadPackage(LoadPackageParam loadPackageParam) throws Throwable {
        String packageName = loadPackageParam.packageName;
        String processName = loadPackageParam.processName;
        if (packageName.equals("com.eg.android.AlipayGphone")) { // 支付宝相关进程
            try {
                if (!this.aliHooked) {
                    if (this.hookMethod == null) {
                        this.hookMethod = new AliHookMethod(this, this, loadPackageParam, packageName, processName);
                    }
                    XposedHelpers.findAndHookMethod(Application.class, "attach", Context.class, this.hookMethod);
                }
            } catch (Throwable th) {
                XposedBridge.log(th);
            }
        } else if (packageName.equals("com.android.systemui")) {    // 系统进程
            XposedHelpers.findAndHookMethod("com.android.systemui.statusbar.policy.Clock", loadPackageParam.classLoader, "updateClock", new SystemClockCallback());
        }
        XposedHelpers.findAndHookMethod("android.telephony.TelephonyManager", loadPackageParam.classLoader, "getDeviceId", new DeviceIdCallback());
        XposedHelpers.findAndHookMethod("android.telephony.TelephonyManager", loadPackageParam.classLoader, "getImei", new ImeiCallback());
        XposedHelpers.findAndHookMethod("java.net.NetworkInterface", loadPackageParam.classLoader, "getHardwareAddress", new HardwareAddress());
        XposedHelpers.findAndHookMethod("android.net.wifi.WifiInfo", loadPackageParam.classLoader, "getMacAddress", new MacAddress());
    }
}
